<?php
/**
 * 输入经纬度，返回地理文本
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2017/9/29
 * Time: 17:24
 */

namespace app\librarys;


class RegeoSdk
{

    public static function getArea($lat,$lon){
        $tmp = $lat .','.$lon;
        $host = "http://regeo.market.alicloudapi.com";
        $path = "/v3/geocode/regeo";
        $method = "GET";
        $appcode = "37b9fc55db4643eb952ad70fc45d3cfe";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
//        $querys = "batch=false&callback=callback&extensions=base&homeorcorp=homeorcorp&location=location&output=output&radius=1000&roadlevel=roadlevel";
        $querys = "location=$tmp";

        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result = curl_exec($curl);
//        var_dump($result);
//        $result = substr($result,1128);
        $item = json_decode($result);
//        var_dump($item);

//        var_dump($item->status);
        if($item->status != 1)
        {
            return -1;
        }
//        var_dump($item['province']);
//        var_dump('zzzzzzz:'.$item->regeocode->addressComponent->province);
        return $item->regeocode->addressComponent->province;
  }
}